package com.situ.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;

public class OrderedBoradcastActivity extends Activity {
    private static  final  String ORDERED_ACTION = "ordered_action";

    private Button mBtnSend;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ordered_broadcast);

        registerReceiver();

        mBtnSend = findViewById(R.id.btn_send_ordered_broadcast);
        mBtnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(ORDERED_ACTION);
                sendOrderedBroadcast(intent, null);
            }
        });

    }

    /**
     * 注册有序广播
     * 有多个接受者
     */
    private void registerReceiver() {
        IntentFilter filter1 = new IntentFilter();
        filter1.addAction(ORDERED_ACTION);
        filter1.setPriority(1000);
        this.registerReceiver(new MyBroadcastReceiverOne(), filter1);

        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(ORDERED_ACTION);
        filter2.setPriority(999);
        this.registerReceiver(new MyBroadcastReceiverTwo(), filter2);

        IntentFilter filter3 = new IntentFilter();
        filter3.addAction(ORDERED_ACTION);
        filter3.setPriority(998);
        this.registerReceiver(new MyBroadcastReceiverThree(), filter3);
    }
}
